using Spine;
using UnityEngine;

[ExecuteInEditMode]
[AddComponentMenu("Spine/SkeletonAnimation")]
public class SkeletonAnimation : SkeletonRenderer, ISkeletonAnimation
{
	public float timeScale = 1f;

	public bool loop;

	public Spine.AnimationState state;

	[SerializeField]
	private string _animationName;

	public Skeleton Skeleton => skeleton;

	public string AnimationName
	{
		get
		{
			return state.GetCurrent(0)?.Animation.Name;
		}
		set
		{
			if (!(_animationName == value))
			{
				_animationName = value;
				if (value == null || value.Length == 0)
				{
					state.ClearTrack(0);
				}
				else
				{
					state.SetAnimation(0, value, loop);
				}
			}
		}
	}

	public event UpdateBonesDelegate UpdateLocal;

	public event UpdateBonesDelegate UpdateWorld;

	public event UpdateBonesDelegate UpdateComplete;

	protected event UpdateBonesDelegate _UpdateLocal;

	protected event UpdateBonesDelegate _UpdateWorld;

	protected event UpdateBonesDelegate _UpdateComplete;

	public override void Reset()
	{
		base.Reset();
		if (valid)
		{
			state = new Spine.AnimationState(skeletonDataAsset.GetAnimationStateData());
			if (_animationName != null && _animationName.Length > 0)
			{
				state.SetAnimation(0, _animationName, loop);
				Update(0f);
			}
		}
	}

	public virtual void Update()
	{
		Update(Time.deltaTime);
	}

	public virtual void Update(float deltaTime)
	{
		if (valid)
		{
			deltaTime *= timeScale;
			skeleton.Update(deltaTime);
			state.Update(deltaTime);
			state.Apply(skeleton);
			if (this._UpdateLocal != null)
			{
				this._UpdateLocal(this);
			}
			skeleton.UpdateWorldTransform();
			if (this._UpdateWorld != null)
			{
				this._UpdateWorld(this);
				skeleton.UpdateWorldTransform();
			}
			if (this._UpdateComplete != null)
			{
				this._UpdateComplete(this);
			}
		}
	}
}
